﻿using System;
using SmartSiteManagementSystem.IRepository.Admin;
using SmartSiteManagementSystem.Models.Admin;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.Admin
{
    public class AdminUserGroupMenuInfoRepository : IAdminUserGroupMenuInfoRepository
    //,IDisposable
    {
        #region dataTable
        /*
create table `admin_user_group_menu`(
	`admin_uer_group_menuid` int not null auto_increment primary key,
	`group_id` int not null ,
    `menu_id` int not null 
    )default charset=utf8;
         */
        #endregion
       // private IDbConnection _readConn;
        //private IDbConnection _writeConn;

        public IList<AdminUserGroupMenuInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {
            IList<AdminUserGroupMenuInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"select admin_uer_group_menuid as AdminUserGroupMenuId,group_id as AdminUserGroupId,menu_id as AdminMenuId from admin_user_group_menu";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("admin_uer_group_menuid"))
                    {
                        pars.Add("admin_uer_group_menuid", param["admin_uer_group_menuid"]);
                        whereCondition.Append("and admin_uer_group_menuid =@admin_uer_group_menuid");
                    }
                    if (param.ContainsKey("group_id"))
                    {
                        pars.Add("group_id", param["group_id"]);
                        whereCondition.Append(" and group_id =@group_id");
                    }
                    if (param.ContainsKey("menu_id"))
                    {
                        pars.Add("menu_id", param["menu_id"]);
                        whereCondition.Append(" and menu_id =@menu_id");
                    }

                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by admin_uer_group_menuid asc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<AdminUserGroupMenuInfo>(execSql, pars).AsList<AdminUserGroupMenuInfo>();
            }
            return back;
        }


        /// <summary>
        /// 事务处理
        /// </summary>
        /// <param name="adminUserGroupId"></param>
        /// <param name="maugmList"></param>
        /// <param name="mgbrList"></param>
        public void UpdateGroupMenu(int adminUserGroupId, IList<AdminUserGroupMenuInfo> maugmList, IList<AdminGroupBtnRelInfo> mgbrList)
        {
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {
                _writeConn.Open();
                using (IDbTransaction transaction = _writeConn.BeginTransaction())
                {
                    try
                    {
                        string deleteAdminUserGroupMenuSql = "delete from admin_user_group_menu where group_id=@group_id";
                        DynamicParameters deleteAdminUserGroupMenuSqlPars = new DynamicParameters();
                        deleteAdminUserGroupMenuSqlPars.Add("group_id", adminUserGroupId);
                        _writeConn.Execute(deleteAdminUserGroupMenuSql, deleteAdminUserGroupMenuSqlPars, transaction);

                        string deleteAdminGroupBtnRelSql = "delete from admin_group_btnrel where admin_user_groupid=@admin_user_groupid";
                        DynamicParameters deleteAdminGroupBtnRelSqlPars = new DynamicParameters();
                        deleteAdminGroupBtnRelSqlPars.Add("admin_user_groupid", adminUserGroupId);
                        _writeConn.Execute(deleteAdminGroupBtnRelSql, deleteAdminGroupBtnRelSqlPars, transaction);

                        if (maugmList != null && maugmList.Count > 0)
                        {
                            foreach (AdminUserGroupMenuInfo item in maugmList)
                            {
                                string inserAdminUserGroupMenuSql = "INSERT INTO admin_user_group_menu(group_id,menu_id)VALUES(@group_id,@menu_id);select last_insert_id();";
                                _writeConn.QueryFirst<int>(inserAdminUserGroupMenuSql, new
                                {
                                    group_id = item.AdminUserGroupId,
                                    menu_id = item.AdminMenuId
                                });
                            }
                        }
                        if (mgbrList != null && mgbrList.Count > 0)
                        {
                            foreach (AdminGroupBtnRelInfo item in mgbrList)
                            {
                                string inserAdminGroupBtnRelSql = "INSERT INTO admin_group_btnrel(admin_user_groupid,btn_id,admin_menuid)VALUES(@admin_user_groupid,@btn_id,@admin_menuid)";
                                _writeConn.QueryFirst<int>(inserAdminGroupBtnRelSql, new
                                {
                                    admin_user_groupid = item.AdminUserGroupId,
                                    btn_id = item.BtnId,
                                    admin_menuid = item.AdminMenuId
                                });
                            }
                        }
                        string updateAdminGroupBtnRelSql = "update  admin_group_btnrel agbr INNER JOIN admin_pagebtn apb ON agbr.btn_id=apb.btn_id set agbr.admin_menuid = apb.admin_menu_id where agbr.admin_user_groupid = @admin_user_groupid";
                        DynamicParameters updateAdminGroupBtnRelSqlPars = new DynamicParameters();
                        updateAdminGroupBtnRelSqlPars.Add("admin_user_groupid", adminUserGroupId);
                        _writeConn.Execute(updateAdminGroupBtnRelSql, updateAdminGroupBtnRelSqlPars, transaction);
                        transaction.Commit();

                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }

                }
            }
        }

    }
}

